<template>
    <div class="container">
        <div>
            <el-dialog :title="'提示'" v-model="dialogVisible" width="30%">
                <div>
                    <el-input v-model="code1" placeholder="请输入原邮箱验证码" autocomplete="off"></el-input>
                    <el-input v-model="code2" placeholder="请输入新邮箱验证码" autocomplete="off"
                        style="margin-top: 20px;"></el-input>
                </div>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="confirm">确 定</el-button>
                </span>
            </el-dialog>

            <el-upload class="el-upload" action="/api/user/upload" :headers="headers" :show-file-list="false"
                :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">


                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </div>

        <div class="S_box">
            <el-form>
                <el-form-item class="button">
                    修改邮箱和密码先点这里
                    <el-button type="primary" @click="getCode" round> (￣︶￣)</el-button>
                </el-form-item>
                <el-form-item>
                    <el-input placeholder="请输入修改后的昵称" v-model="acName">
                        <template v-slot:prepend>昵称：</template>
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-input placeholder="请输入修改后的密码" v-model="password">
                        <template v-slot:prepend>密码：</template>
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-input placeholder="请输入修改后的生日日期" v-model="birthday">
                        <template v-slot:prepend>生日：</template>
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-input placeholder="请输入修改后的电子邮箱" @blur="validateEmail" v-model="mail">
                        <template v-slot:prepend>电子邮箱：</template>
                    </el-input>
                    <span v-if="mailError" style="color: red; margin-left: 10px;">{{ mailError }}</span>
                </el-form-item>
                <el-form-item>
                    <el-input placeholder="请输入修改后的电话号码" v-model="tel">
                        <template v-slot:prepend>电话号码：</template>
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-input placeholder="请输入修改后的个性签名" v-model="person_signature">
                        <template v-slot="prepend">个性签名：</template>
                    </el-input>
                </el-form-item>

                <el-form-item class="button">
                    <el-button type="primary" @click="showDialog" round>修改邮箱</el-button>
                    <el-button type="primary" @click="Resetpw" round>修改密码</el-button>
                    <el-button type="primary" @click="doReset" round>保存</el-button>
                    <el-button type="danger" @click="logoff" round>注销账号</el-button>
                </el-form-item>
            </el-form>
        </div>

    </div>
</template>

<script>
import axios from "axios";

export default {
    data() {
        return {
            headers: {
                // Authorization: this.getToken(),
                "HOSHINO_T_KK": localStorage.getItem("userToken")
            },
            dialogVisible: false,
            code1: '',
            code2: '',
            mailError: '',
            personalobject: Object,
            acName: '',
            password: '',
            mail: '',
            tel: '',
            url: '',
            person_signature: '',
            imageUrl: '',
            birthday: '',
            token: localStorage.getItem("userToken")
        };
    },
    created() {
        // const userId = localStorage.getItem('userId');
        axios({
            url: "/api/user/UserDetailsById",
            method: 'post',
            headers: {
                "HOSHINO_T_KK": localStorage.getItem("userToken"),
            },
            data: {
                user_id: localStorage.getItem('userpageId')
            }
        })
            .then(response => {
                console.log(response);

                this.acName = response.data.data.acName;
                this.mail = response.data.data.mail;
                this.tel = response.data.data.tel;
                this.url = response.data.data.url;
                this.person_signature = response.data.data.personSignature;
                this.birthday = response.data.data.birthday;
                // this.user_id=response.data.data.userDetails.userId;

            })
            .catch(error => {
                console.error('Error ', error);
            })


    },
    methods: {
        logoff() {
            this.$confirm('此操作将永久注销该账号, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                axios({
                    url: "/api/user/revoke",
                    method: 'post',
                    headers: {
                        "HOSHINO_T_KK": localStorage.getItem("userToken"),
                    },
                    data: {
                        code: this.code1,
                        new_code: this.code2,
                        new_mail: this.mail
                    }
                })
                    .then(response => {
                        console.log(response);
                        console.log(response.data.data);
                        this.mail = response.data.data;
                    })
                    .catch(error => {
                        console.error('Error ', error);
                    })
                this.$message({
                    type: 'success',
                    message: '注销成功!'
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消注销'
                });
            })
        },
        showDialog() {
            this.dialogVisible = true;
            console.log(this.dialogVisible);
            // 可以在这里重置输入框的值
            this.code1 = '';
            this.code2 = '';
        },
        handleClose() {
            this.dialogVisible = false;
        },
        confirm() {
            if (this.code1 && this.code2) { // 假设两个验证码都需要填写
                this.$message({
                    type: 'success',
                    message: '修改成功'
                });
                axios({
                    url: "/api/user/reset/mail",
                    method: 'post',
                    headers: {
                        "HOSHINO_T_KK": localStorage.getItem("userToken"),
                    },
                    data: {
                        code: this.code1,
                        new_code: this.code2,
                        new_mail: this.mail
                    }
                })
                    .then(response => {
                        console.log(response);
                        console.log(response.data.data);
                        this.mail = response.data.data;
                    })
                    .catch(error => {
                        console.error('Error ', error);
                    })
            } else {
                this.$message({
                    type: 'warning',
                    message: '请填写所有验证码'
                });
            }
            this.dialogVisible = false;
        },

        getCode() {
            axios({
                url: "/api/mail/requestCode",
                method: 'post',
                headers: {
                    "HOSHINO_T_KK": localStorage.getItem("userToken"),
                },
                data: {
                    mail: this.mail
                }
            })
                .then(response => {
                    console.log(response);
                })
                .catch(error => {
                    console.error('Error ', error);
                })

        },
        validateEmail() {
            const emailRegex = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
            if (!emailRegex.test(this.mail)) {
                this.mailError = '邮箱格式不正确';
            } else {
                this.mailError = '';
            }
        },

        handleAvatarSuccess(res, file) {
            this.imageUrl = URL.createObjectURL(file.raw);
            this.url = res.url;
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        Resetpw() {
            this.$prompt('请输入验证码', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
            }).then(({ value }) => {
                this.code = value;
                axios({
                    url: "/api/user/reset/password",
                    method: 'post',
                    headers: {
                        "HOSHINO_T_KK": localStorage.getItem("userToken"),
                    },
                    data: {
                        code: this.code,
                        password: this.password
                    }
                })
                    .then(response => {
                        console.log(response);

                    })
                    .catch(error => {
                        console.error('Error ', error);
                    })


                this.$message({
                    type: 'success',
                    message: '修改成功 '
                });


            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消修改'
                });
            });

        },
        doReset() {
            axios({
                url: "/api/user/reset/newDetails",
                method: 'post',
                headers: {
                    "HOSHINO_T_KK": localStorage.getItem("userToken"),
                },
                data: {
                    ac_name: this.acName,
                    tel: this.tel,
                    person_signature: this.person_signature,
                    birthday: this.birthday,
                }
            })
                .then(response => {
                    console.log(response);
                    this.personalobject = response.data.data;
                    this.acName = response.data.data.acName;
                    this.birthday = response.data.data.birthday;
                    this.tel = response.data.data.tel;
                    this.person_signature = response.data.data.personSignature;
                    const userId = localStorage.getItem('userId');
                    axios({
                        url: "/api/user/UserDetailsById",
                        method: 'post',
                        headers: {
                            "HOSHINO_T_KK": localStorage.getItem("userToken"),
                        },
                        data: {
                            user_id: userId
                        }
                    })
                        .then(response => {
                            console.log(response);

                            this.acName = response.data.data.acName;
                            this.mail = response.data.data.mail;
                            this.tel = response.data.data.tel;
                            this.birthday = response.data.data.birthday;
                            this.url = response.data.data.url;
                            this.person_signature = response.data.data.personSignature;


                        })
                        .catch(error => {
                            console.error('Error ', error);
                        })

                })
                .catch(error => {
                    console.error('Error ', error);
                })

        }


    }
}





</script>

<style scoped>
.container {
    color: black;
    background-color: white;
    height: 100%;
    width: 90%;

}

.button {
    display: flex;
    justify-content: flex-end;
}

.container {
    background-color: white;
    height: 100%;
    width: 90%;

}

.S_box {
    height: 50%;
    width: 40%;
    margin: auto;
    transform: translate(0, 30%);

}

.el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    top: 50%;
    left: 50%;
    transform: translate(-50%, 30%);
    position: relative;
    overflow: hidden;
    margin: auto;
}

.el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;

}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
    margin: auto;
}
</style>
